//初始化同步属性数据
function initPropertyList($propertyList, sourceFields, targetFields){
	var html = '';
	var sLen = sourceFields.length;
	var tLen = targetFields.length;
	if(sLen > 0 && sLen == tLen){
		//遍历初始化同步列表
		for (var i = 0; i < sLen; i++) {
			var sValue = sourceFields[i]['name'];
			var sType = sourceFields[i]['type'];
			var sTypeName = sourceFields[i]["typeName"];
			
			var tValue = targetFields[i]['name'];
			var tType = targetFields[i]['type'];
			var tTypeName = targetFields[i]["typeName"];
			var tr = '';
			tr += "<tr>";
			tr += "<td type='"+sType+"' typeName='"+sTypeName+"'>"+sValue+"</td>";
			tr += "<td type='"+tType+"' typeName='"+tTypeName+"'>"+tValue+"</td>";
			tr += "<td><a class='fa fa-remove fa-2x propertyDelete fastsyncer_pointer' title='删除' ></a></td>";
			tr += "</tr>";
			html += tr;
		}
	}
	//将组装好的数据渲染给页面
	$propertyList.html(html);
	
	//删除同步属性按钮事件
	bindEventForDeleteProperty($(".propertyDelete"));
}

//添加同步属性按钮事件
function addProperty($propertyList, sourceField, targetField){
	var sValue = sourceField.attr("value");
	var sType = sourceField.attr("type");
	var sTypeName = sourceField.attr("typeName");
	var tValue = targetField.attr("value");
	var tType = targetField.attr("type");
	var tTypeName = targetField.attr("typeName");
	//校验唯一性
	if(!checkPropertyUnique($propertyList, sValue, tValue)){
		bootGrowl('已添加该属性.',"danger"); 
		return;
	}
	var html = '';
	html += "<tr>";
	html += "<td type='"+ sType +"' typeName='"+sTypeName+"'>"+sValue+"</td>";
	html += "<td type='"+ tType +"' typeName='"+tTypeName+"'>"+tValue+"</td>";
	html += "<td><a class='fa fa-remove fa-2x propertyDelete fastsyncer_pointer' title='删除' ></a></td>";
	html += "</tr>";
	$propertyList.append(html);
}

//校验唯一性
function checkPropertyUnique($propertyList, sValue, tValue){
	var isOk = true;
	var trs = $propertyList.find("tr");
	trs.each(function(i,o){
		var sField = $(this).find("td:eq(0)").text();
		var tField = $(this).find("td:eq(1)").text();
		//如果有重复添加，返回false
		if(sValue==sField && tValue==tField ){
			isOk = false;
			//结束校验
			return false;
		}
	});
	return isOk;
}

//删除同步属性按钮事件
function bindEventForDeleteProperty($propertyDelete){
	//删除同步属性按钮事件
	$propertyDelete.click(function(){
		var tr = $(this).parent().parent();
		tr.remove();
	});
}

//初始化同步属性数据
function bindChangeEventForPropertyColumn($this, columns){
	var colLen = columns.length;
	var opts = "";
	for (var i = 0; i < colLen; i++) {
		var col = columns[i];
		var name = col['name'];
		var type = col['type'];
		var typeName = col['typeName'];
		opts += "<option value='"+ name +"' type='"+ type +"' typeName='"+ typeName +"' >"+ name +" ("+typeName+")</option>";
	}
	$this.html(opts);
}

$(function() {
	var $sourceCols = $("#propertySourceCols");
	var $targetCols = $("#propertyTargetCols");
	var $propertyList = $("#propertyList");
	var $propertyAdd = $("#propertyAdd");
	var $propertyDelete = $(".propertyDelete");
	
	//绑定数据源下拉切换事件
	$sourceCols.change(function(){
		$targetCols.find("option").removeAttr("selected");
		//选中的数据源字段
		var sourceVal = $(this).find(":selected").val();
		//自动选中目标源类似字段
		$targetCols.find("option").each(function(i,o){
			if(sourceVal==$(this).val()){
				$(this).attr("selected",true);
				return false;
			}
		});
	});
	//初始化同步属性数据
	//数据源
	bindChangeEventForPropertyColumn($sourceCols, driver.sourceConfig.column);
	//目标源
	bindChangeEventForPropertyColumn($targetCols, driver.targetConfig.column);
	//初始化已有属性列表
	initPropertyList($propertyList, driver.sourceFileds, driver.targetFileds);
	
	//同步属性按钮添加事件
	$propertyAdd.click(function(){
		var sourceField = $sourceCols.find(":selected");
		var targetField = $targetCols.find(":selected");
		addProperty($propertyList, sourceField, targetField);
		
		//重新绑定按钮删除事件
		bindEventForDeleteProperty($(".propertyDelete"));
	});
	
})
